//
//  PushAnimationView.m
//  MONO
//
//  Created by 王松锋 on 2019/6/11.
//  Copyright © 2019 王松锋. All rights reserved.
//

#import "PushAnimationView.h"
#import <AVFoundation/AVFoundation.h>

@interface PushAnimationView()
{
    UILabel * _titleLable;
    UILabel * _contentLable;
    UIButton * _besidesBtn;
    UILabel * _bottomLable;
}
@property(nonatomic,strong)AVPlayer * player;
@property(nonatomic,weak)AVPlayerLayer * playerLayer;
@property(nonatomic,strong)AVAudioPlayer * musicPlayer;

@end
@implementation PushAnimationView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self==[super initWithFrame:frame]) {
        [self setupView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)setupView{
    
    AVPlayer * player = [[AVPlayer alloc]initWithURL:[[NSBundle mainBundle]URLForResource:@"PushAnimation.mp4" withExtension:nil]];
    player.volume = 0.0;
    player.volume = 3.0;
    self.player = player;
    
    AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.videoGravity = AVLayerVideoGravityResize;
    playerLayer.frame = CGRectMake(0, SCREEN_HEIGHT-90-SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_WIDTH);
    [self.layer addSublayer:playerLayer];
    self.playerLayer = playerLayer;
    
    self.musicPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:@"pushsound.aiff" withExtension:nil] error:nil];
    self.musicPlayer.numberOfLoops = 1;
    [self.musicPlayer setVolume:1.0];
    [self.musicPlayer prepareToPlay];
    
    _titleLable = [UILabel new];
    _titleLable.text = @"开启推送,不错过感兴趣的好内容";
    _titleLable.textColor = [UIColor blackColor];
    _titleLable.numberOfLines = 0;
    _titleLable.font = [UIFont systemFontOfSize:22 weight:UIFontWeightHeavy];
    
    _contentLable = [UILabel new];
    
    _contentLable.text = @"MONO为你提供了主题站更新推送功能，在主题站更新时会第一时间告诉你。";
    _contentLable.textColor = [UIColor blackColor];
    _contentLable.numberOfLines = 0;
    _contentLable.font = [UIFont systemFontOfSize:19 weight:UIFontWeightLight];
    _contentLable.textAlignment = NSTextAlignmentCenter;
    
    _besidesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _besidesBtn.titleLabel.font = REGULARFONT(14);
    [_besidesBtn setTitle:@"以后再说" forState:UIControlStateNormal];
    [_besidesBtn setTitleColor:[UIColor colorWithRed:0.62 green:0.62 blue:0.62 alpha:1] forState:UIControlStateNormal];
    
    
    _bottomLable = [UILabel new];
    _bottomLable.font = LIGHTFONT(12);
    _bottomLable.text = @"反正以后我们也会时常提醒您的O(∩_∩)O";
    _bottomLable.textAlignment = NSTextAlignmentCenter;
    _bottomLable.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
    
    NSArray *views = @[_titleLable,_contentLable,_besidesBtn,_bottomLable];
    [self sd_addSubviews:views];
    
    _bottomLable.sd_layout
    .centerXEqualToView(self)
    .bottomSpaceToView(self, 20)
    .heightIs(_bottomLable.font.lineHeight);
    [_bottomLable setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH];
    
    _besidesBtn.sd_layout
    .centerXEqualToView(_bottomLable)
    .bottomSpaceToView(_bottomLable, 10)
    .heightIs(_besidesBtn.titleLabel.font.lineHeight)
    .widthIs(200);
    
    
    _titleLable.sd_layout
    .centerXEqualToView(self)
    .topSpaceToView(self, 60)
    .autoHeightRatio(0);
    [_titleLable setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH - 40];
    
    _contentLable.sd_layout
    .centerXEqualToView(_titleLable)
    .topSpaceToView(_titleLable, 30)
    .autoHeightRatio(0);
    [_contentLable setSingleLineAutoResizeWithMaxWidth:SCREEN_WIDTH - 20];
    
    [_besidesBtn addTarget:self action:@selector(enterMainAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.player play];
    [self.musicPlayer play];
}
- (void)enterMainAction:(UIButton *)button{
    [self.player pause];
    [self.musicPlayer pause];
    self.musicPlayer = nil;
    self.player = nil;
    self.playerLayer = nil;
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
@end
